using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using OhioState.AssetManaged;
using OhioState.Graphics.Proxy;

namespace OhioState.Controls
{
    public partial class MaterialEditorCtrl : UserControl
    {
        //
        // Pointer to the instance of the asset manager
        //
        private AssetManager assetMan;

        //
        // List of templates
        //
        List<MaterialProxy> templates;

        //
        // Event to trigger invalidate
        //
        public event IAssetManagedHandler ChangedEvent;

        //
        // Event helper for firing events
        //
        protected EventsStrategyAbstract _eventsHelper;

        //
        // Constructor sets up all the object and events
        //
        public MaterialEditorCtrl()
        {
            InitializeComponent();
            //
            // Get a pointer to the instance of the asset manager and create templates list
            //
            assetMan = AssetManager.Instance;
            _eventsHelper = new EventsStrategyAsync();

            templates = new List<MaterialProxy>();
            //
            // Set up event handler for selection changed event
            //
            materialManagerControl.SelectedEvent += new IAssetManagedListHandler(materialManagerControl_SelectedEvent);
            materialManagerControl.UpdatedEvent += new IAssetManagedHandler(materialManagerControl_UpdatedEvent);

            //
            // Initialize the control
            //
            //materialManagerControl.Init(typeof(MaterialProxy));

            List<Type> types = new List<Type>();
            types.Add(typeof(MaterialProxy));
            materialManagerControl.Init(types);
            
            //
            // Set up the drop down for the templates
            //
            BuiltTemplateDropDown();
        }

        #region Public properties

        //
        // Public property to set the orientation - vertical or horizontal
        //
        public Orientation Orientation
        {
            get { return splitContainer1.Orientation; }
            set
            {
                splitContainer1.Orientation = value;
            }
        }

        #endregion

        //
        // Setup the drop down list of all templates
        //  Clear the Drop down menu and then add each item from the template list to the
        //  drop down menu.  
        //
        private void BuiltTemplateDropDown()
        {
            newToolStripButton.DropDownItems.Clear();
            Image image = new Bitmap(32, 32); // global::ComponentControls.Properties.Resources.Material_icon;
            newToolStripButton.DropDownItems.Add("Material", image, CreateNewFromTemplate);
        }

        //
        // Gets a material from the template list given its name
        //  or returns null if it doesn't exist.
        //
        private MaterialProxy GetTemplate(String name)
        {
            foreach (MaterialProxy material in templates)
            {
                if (material.Name == name)
                {
                    return material;
                }
            }
            return null;
        }

        //
        // Event handler for Template Drop down clicks
        //  Make a new material based on the type
        //
        public void CreateNewFromTemplate(Object sender, EventArgs e)
        {
            if (sender.GetType() == typeof(ToolStripMenuItem))
            {
                String name = ((ToolStripMenuItem)sender).Text;
                if (name == "Material")
                {
                    IAssetManaged obj = assetMan.Add(new MaterialProxy("Material"));
                }
            }
        }

        //
        // Event handler for selection changed event
        //  This changes the material that the material control currently controls
        //
        public void materialManagerControl_SelectedEvent(IList<IAssetManaged> items)
        {
            if (items.Count > 0)
            {
                materialControl1.SetMaterial((MaterialProxy)items[0]);
            }
        }

        //
        // Event handler for item updated event.  This fires an event to tell
        //  the opengl panel to be invalidated.
        //
        public void materialManagerControl_UpdatedEvent(IAssetManaged obj)
        {
            _eventsHelper.Fire(ChangedEvent, obj);
        }
        //
        // New button - Create a new material for the user to edit
        //
        private void newButton_Click(object sender, EventArgs e)
        {
            MaterialProxy mat;
            if (materialManagerControl.GetSelected().Count > 0)
            {
                mat = (MaterialProxy)((MaterialProxy)materialManagerControl.GetSelected()[0]).Clone();
                mat.SetName(mat.Name);
            }
            else
            {
                mat = new MaterialProxy("Untitled");
            }
            IAssetManaged obj = assetMan.Add(mat);
        }
        //
        // Remove button - remove the currently selected material from the asset
        //  manager. 
        //
        private void removeButton_Click(object sender, EventArgs e)
        {
            List<IAssetManaged> list = materialManagerControl.GetSelected();
            foreach (IAssetManaged obj in list)
            {
                assetMan.Remove(obj);
            }
        }

        //
        // Open button - calls a dialog to open a file that contains a material,
        //  and adds it to the asset manager.  If there is a name,type collision then the new
        //  object does not get added.  Instead the object that already exists gets selected
        //
        private void openButton_Click(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                MaterialProxy material = MaterialProxy.LoadFromFile(openFileDialog.FileName);
                if (material != null)
                {
                    IAssetManaged obj = assetMan.Add(material);
                    // check for duplicate
                    if (obj == material)
                    {
                        // add the new item to the template list and the template drop down if it doesn't
                        //  already exist
                        if (GetTemplate(material.Name) == null)
                        {
                            templates.Add((MaterialProxy)obj);
                            BuiltTemplateDropDown();
                        }
                    }
                }
            }
        }

        //
        // Save button - saves the currently selected material to a file.
        //
        private void saveButton_Click(object sender, EventArgs e)
        {
            if (materialManagerControl.GetSelected().Count > 0)
            {
                MaterialProxy obj = (MaterialProxy)materialManagerControl.GetSelected()[0];
                // No more filename assistance
                /*String filename = assetMan.GetFilename(obj);
                if (filename != null)
                {
                    saveFileDialog.FileName = filename;
                }
                else
                {
                    saveFileDialog.FileName = obj.Name;
                }*/
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    obj.SaveToFile(saveFileDialog.FileName);
                }
            }
        }


    }
}
